# -*- coding = utf-8 -*-
# @Time : 2022/3/11 9:37
# @Author :Henry L
# @File : nash.py
# @Software : PyCharm
import csv
import json
import math
import random
import time
import execjs
import requests
import logging
from sqlalchemy import create_engine
import pandas as pd


logging.basicConfig(level=logging.DEBUG,filename='xiangmu.log',format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') #设置日志级别
"""注意这里我改了execjs中文件的编码格式"""
regn_code_url = "https://fuwu.nhsa.gov.cn/ebus/fuwu/api/nthl/api/dic/queryAdmdvsTree"
lv_and_type_url = "https://fuwu.nhsa.gov.cn/ebus/fuwu/api/nthl/api/fixed/queryDicByType"
# result_url = "https://fuwu.nhsa.gov.cn/ebus/fuwu/api/nthl/api/fixed/queryFixedHospital"
result_url = "https://fuwu.nhsa.gov.cn/ebus/fuwu/api/nthl/api/CommQuery/queryWmTcmpatInfoBFromEs"
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"

with open('nash.js', 'r', encoding='utf-8') as f:
    nhsa_js = execjs.compile(f.read())


def get_headers():
    """获取 header 参数，每次请求改变"""
    headers = nhsa_js.call("getHeaders")
    headers["User-Agent"] = UA
    headers["Content-Type"] = "application/json"
    headers["Host"] = "fuwu.nhsa.gov.cn"
    headers["Origin"] = "https://fuwu.nhsa.gov.cn"
    headers["Referer"] = "https://fuwu.nhsa.gov.cn/nationalHallSt/"
    # print(headers)
    return headers


def get_regn_code():
    """获取城市代码，返回结果无加密"""
    payload = {"data": {"transferFlag": ""}}
    response = requests.post(url=regn_code_url, json=payload, headers=get_headers())
    region_dic = json.loads(response.text)
    # print(region_dic)
    sale_num = region_dic.get("data")
    # print(sale_num)
    # sale_num1 = sale_num.get("code")
    for i in sale_num:
        a = i['code']
        c = i['name']
        b = i['list']
        csvFile = open('yaodian.csv', 'a', encoding='utf-8', newline='')
        writer = csv.writer(csvFile)
        writer.writerow([a, c])
        for n in b:
            # print(n)
            # for n in m:
            a = n['code']
            c = n['name']
            # print(a)
            csvFile = open('yaodian.csv', 'a', encoding='utf-8', newline='')
            writer = csv.writer(csvFile)
            writer.writerow([a, c])
        # print(a)
        # print(b)
    # sale_num = region_dic.get("data")
    # csvFile = open('yaodian.csv', 'a', encoding='utf-8', newline='')
    # writer = csv.writer(csvFile)
    # writer.writerow([ip, port, anonymous, type, location, speed, date])
    return region_dic['data']



def get_medins_lv_or_type_code(key):
    """获取医疗机构等级 (LV) or 类型 (TYPE) 代码"""
    if key == "LV":
        payload = {"type": "MEDINSLV"}
    elif key == "TYPE":
        payload = {"type": "MEDINS_TYPE"}   
    else:
        print("输入有误!")
        return
    encrypted_payload = nhsa_js.call("getEncryptedData", payload)
    encrypted_data = requests.post(url=lv_and_type_url, json=encrypted_payload, headers=get_headers()).json()
    decrypted_data = nhsa_js.call("getDecryptedData", encrypted_data)
    return decrypted_data

def get_result(region,pagenum):
    requests.adapters.DEFAULT_RETRIES = 15
    s = requests.session()
    s.keep_alive = False
    addr = ""
    openElec = ""
    medins_lv_code = ""
    medins_name = ""
    medins_type_code = ""
    regn_code = region

    payload = {
        "addr": addr,
        "medinsName": medins_name,
        "openElec": openElec,
        "pageNum": pagenum,
        "pageSize": 100,
        "regnCode": regn_code,
    }
    encrypted_payload = nhsa_js.call("getEncryptedData", payload)
    sleep_time = 0.1 * random.randint(1, 20)
    time.sleep(random.randint(10, 20) + sleep_time)
    encrypted_data = requests.post(url=result_url, json=encrypted_payload, headers=get_headers(), timeout=60).json()
    while encrypted_data['code'] != 0:
        time.sleep(random.randint(10, 20) + sleep_time)
        encrypted_data = requests.post(url=result_url, json=encrypted_payload, headers=get_headers(), timeout=60).json()
    decrypted_data = nhsa_js.call("getDecryptedData", encrypted_data)
    return decrypted_data


def to_csv(result, writer, province, city):
    for row in result:
        row["province"] = province
        if city != '':
            row["city"] = city
        writer.writerow(row)


def main():

    with open('data1.csv', 'a+', encoding='utf-8', newline='') as file:
        # header = ['medinsTypeName','medinsNatu','hospLv','medinsLvName','medinsType','medinsLv',
        #           'medinsName','addr','medinsCode','medinslvCode','medinsNatuName','province','city','openElec','lnt','lat'
        #           ,'rtalPhacCode', 'drugBizScp', 'tel']
        header = ['medinsName','addr','province','city','openElec','rtalPhacCode','drugBizScp','tel','lat', 'lnt']
        lv_dict = dict
        type_dict = dict
        wirter = csv.writer(file)
        wirter.writerow(header)
        # 获取城市代码
        w = csv.DictWriter(file, header)
        region_dic = get_regn_code()
        num = 1
        for region in region_dic:
                if num > 32:
                    break
                # if num < 16:
                #     num = num + 1
                #     continue
                if region['name'] == '北京市' or region['name'] == '天津市' or region['name'] == '重庆市' or region['name'] == '新疆生产建设兵团':
                    region_code = region['code']
                    result = get_result(region_code, 1)
                    total_num = result['total']
                    data_ls = result['list']
                    to_csv(data_ls, w, region['name'], region['name'])
                    max_page = math.ceil(total_num / 100)
                    # 默认max_page 是大于2的
                    time.sleep(10)
                    for page in range(2, max_page+1):
                        try:
                            result_n = get_result(region_code, page)
                        except Exception as e:
                            logging.info('the exception error is %s, other is %s' % (e.args, repr(e)))
                            time.sleep(180)
                            continue
                        data_ls_n = result_n['list']
                        to_csv(data_ls_n, w, region['name'], region['name'])
                        logging.info("现在获取的是 %s 第 %s 页" % (region_code, page))
                else:
                    province = region['name']
                    city_ls = region['list']
                    for city in city_ls:
                        try:
                            result = get_result(city['code'], 1)
                        except:
                            time.sleep(245)
                            result = get_result(city['code'], 1)
                        finally:
                            total_num = result['total']
                            data_ls = result['list']
                            to_csv(data_ls, w, province, city['name'])
                            if total_num > 100:
                                max_page = math.ceil(total_num / 100)
                                for page in range(2, max_page + 1):
                                    try:
                                        result_n = get_result(city['code'], page)
                                    except Exception as e:
                                        logging.info('the exception error is %s, other is %s and occur at %s pagenum is %s'  % (e.args, repr(e), city, page))
                                        time.sleep(240)
                                        continue
                                    data_ls_n = result_n['list']
                                    to_csv(data_ls_n, w, province, city['name'])
                                    logging.info("现在获取的是 %s 第 %s 页" % (city['code'], page))
                            # else:
                            #     continue
                        time.sleep(60)
                num = num + 1
                time.sleep(300)
    # 获取地区编码
    # region_dic = get_regn_code()
    # 获取医疗机构等级代码
    # level_ls = get_medins_lv_or_type_code("LV")
    # print(level_ls)
    # for i in level_ls:
    #     lv_dict.update({i['value']: i['text']})
    #     print(lv_dict['value'])
    # # 获取医疗机构类型代码
    # type_ls = get_medins_lv_or_type_code("TYPE")
    # print(type_ls)
    # 获取搜索结果







if __name__ == "__main__":
    main()
    time.sleep(10)
    dff = pd.read_csv('data1.csv')
    # engine = create_engine('postgresql://postgres:j3IA6rebQ9NG@192.168.6.226:5432/odp20210825')
    # '''index : bool, default True
    #             Write DataFrame index as a column. Uses `index_label` as the column
    #             name in the table.'''
    # dff.to_sql('yibao_accountbase_' + time.strftime('%Y%m%d', time.localtime(time.time())),
    #            engine, index=False, if_exists='replace')
